﻿using System;
using System.Linq;
using TextProcessingLibrary;

namespace NmlPlaylistExporter
{
    class Program
    {
        static int Main(string[] args)
        {
            string nmlFile = null;
            string outputDir = null;

            // get command-line arguments
            int processed = 0;
            while (processed < args.Length)
            {
                switch (args[processed++])
                {
                    case "-i":
                        if (processed < args.Length)
                            nmlFile = args[processed++];
                        break;
                    case "-o":
                        if (processed < args.Length)
                            outputDir = args[processed++];
                        break;
                }
            }

            if (string.IsNullOrWhiteSpace(nmlFile) || string.IsNullOrWhiteSpace(outputDir))
                return onArgumentsMissing();

            // load tasks from settings
            var tasks = ProcessingTask.Load("settings.xml");
            if (tasks == null || !tasks.Any())
                Console.WriteLine("No Processing will be done");

            // get and export playlists
            try
            {
                PlaylistExporter exporter = new PlaylistExporter(tasks);
                exporter.Export(nmlFile, outputDir);
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 1;
            }
        }


        private static int onArgumentsMissing()
        {
            string[] usage ={   "Arguments incomplete / incorrect!",
                                "Usage:",
                                "\t-i {pathToNml} -o {outputDirectory}"
                            };

            foreach (var line in usage)
                Console.WriteLine(line);

            Console.ReadKey();
            return 1;
        }
    }
}
